package dialog.data;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.HashMap;

import conditional.Conditional;
import configuration.Paths;

import data.DataElement;
import data.DataTools;
import data.DataElement.DataElementType;
import dialog.model.DialogChoice;
import dialog.model.DialogMenu;

/**
 * @deprecated
 * @author Richard
 *
 */
public class DialogMenuDatabase {

	private static DialogMenuDatabase instance;

	private HashMap<String, DialogMenu> menus;

	private DialogMenuDatabase() {
		menus = new HashMap<String, DialogMenu>();
	}

	public static DialogMenuDatabase getInstance() {
		if (instance == null) {
			instance = new DialogMenuDatabase();
		}
		return instance;
	}

	public void updateFromFiles() {
		try {
			File menusFile = new File(Paths.dialogDataPath + File.separator
					+ "dialog_menus.rpg");
			FileInputStream fis = new FileInputStream(menusFile);
			ObjectInputStream ois = new ObjectInputStream(fis);
			DataElement rootElement = (DataElement) ois.readObject();
//			ArrayList<DataElement> menuElements = rootElement
//					.getChildElements();
//			for (DataElement de : menuElements) {
//				DataElement meta = de.getChildElementOfName("Meta Data");
//				DialogMenu menu = new DialogMenu((String) meta
//						.getChildElementOfName("Name").getValue(),
//						(Boolean) meta.getChildElementOfName("Is Root")
//								.getValue());
//				DataElement choices = de.getChildElementOfName("Choices");
//				for (DataElement choiceElement : choices.getChildElements()) {
//					DialogChoice choice = new DialogChoice(
//							(String) choiceElement
//									.getChildElementOfName("Text").getValue());
//					for (DataElement condElement : choiceElement
//							.getChildElementOfName("Conditionals")
//							.getChildElements()) {
////						Conditional cond = new Conditional(condElement);
//						
//					}
//				}
//			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}

	public void save() {
		DataElement root = new DataElement("Dialog Menu Database",
				DataElementType.COMPLEX, null);
		for (String s : menus.keySet()) {
			DialogMenu dm = menus.get(s);
			DataElement dialogMenu = new DataElement("Dialog Menu",
					DataElementType.COMPLEX, null);
			DataElement metaData = new DataElement("Meta Data",
					DataElementType.COMPLEX, null);
			metaData.addChildElement(new DataElement("Name",
					DataElementType.STRING, dm.getName()));
			metaData.addChildElement(new DataElement("Is Root",
					DataElementType.BOOL, dm.isRoot()));
			dialogMenu.addChildElement(metaData);
			DataElement choices = new DataElement("Choices",
					DataElementType.COMPLEX, null);
			for (DialogChoice dc : dm.getChoices()) {
				DataElement dialogChoice = new DataElement("Dialog Choice",
						DataElementType.COMPLEX, null);
				dialogChoice.addChildElement(new DataElement("Text",
						DataElementType.STRING, dc.getText()));
				DataElement conditionals = new DataElement("Conditionals",
						DataElementType.COMPLEX, null);
				for (Conditional c : dc.getConditionals()) {
					conditionals.addChildElement(c.createConditionalElement());
				}
				dialogChoice.addChildElement(conditionals);
				choices.addChildElement(dialogChoice);
			}
			dialogMenu.addChildElement(choices);
			root.addChildElement(dialogMenu);
		}
		try {
			File menusDir = new File(Paths.dialogDataPath + File.separator
					+ "dialog_menus.rpg");
			FileOutputStream fos = new FileOutputStream(menusDir);
			ObjectOutputStream oos = new ObjectOutputStream(fos);
			oos.writeObject(root);
			oos.close();
			fos.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}
